.class public Lcom/melesta/engine/BackupAgent;
.super Landroid/app/backup/BackupAgent;
.source "BackupAgent.java"


# static fields
.field private static final BACKUP:Ljava/lang/String; = "BACKUP"

.field private static final BACKUP_TRANSACTIONS:Ljava/lang/String; = "BACKUP2"


# direct methods
.method public constructor <init>()V
    .locals 2

    .prologue
    .line 19
    invoke-direct {p0}, Landroid/app/backup/BackupAgent;-><init>()V

    .line 20
    const-string v0, "engine"

    const-string v1, "BackupAgent()"

    invoke-static {v0, v1}, Landroid/util/Log;->i(Ljava/lang/String;Ljava/lang/String;)I

    .line 21
    return-void
.end method

.method static backupFiles(Landroid/app/backup/BackupDataOutput;Lcom/melesta/engine/DatabaseHelper;)[B
    .locals 3
    .parameter "data"
    .parameter "dbh"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 50
    invoke-static {p1}, Lcom/melesta/engine/DbBackup;->backupFiles(Lcom/melesta/engine/DatabaseHelper;)[B

    move-result-object v0

    .line 51
    .local v0, buffer:[B
    array-length v1, v0

    .line 52
    .local v1, len:I
    if-eqz p0, :cond_0

    .line 53
    const-string v2, "BACKUP"

    invoke-virtual {p0, v2, v1}, Landroid/app/backup/BackupDataOutput;->writeEntityHeader(Ljava/lang/String;I)I

    .line 54
    invoke-virtual {p0, v0, v1}, Landroid/app/backup/BackupDataOutput;->writeEntityData([BI)I

    .line 57
    :cond_0
    return-object v0
.end method

.method static backupTransactions(Landroid/app/backup/BackupDataOutput;Lcom/melesta/engine/DatabaseHelper;)[B
    .locals 3
    .parameter "data"
    .parameter "dbh"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 61
    invoke-static {p1}, Lcom/melesta/engine/DbBackup;->backupTransactions(Lcom/melesta/engine/DatabaseHelper;)[B

    move-result-object v0

    .line 62
    .local v0, buffer:[B
    array-length v1, v0

    .line 63
    .local v1, len:I
    if-eqz p0, :cond_0

    .line 64
    const-string v2, "BACKUP2"

    invoke-virtual {p0, v2, v1}, Landroid/app/backup/BackupDataOutput;->writeEntityHeader(Ljava/lang/String;I)I

    .line 65
    invoke-virtual {p0, v0, v1}, Landroid/app/backup/BackupDataOutput;->writeEntityData([BI)I

    .line 68
    :cond_0
    return-object v0
.end method

.method private restoreFiles(Landroid/app/backup/BackupDataInput;Lcom/melesta/engine/DatabaseHelper;I)V
    .locals 2
    .parameter "data"
    .parameter "dbh"
    .parameter "dataSize"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 111
    new-array v0, p3, [B

    .line 112
    .local v0, dataBuf:[B
    const/4 v1, 0x0

    invoke-virtual {p1, v0, v1, p3}, Landroid/app/backup/BackupDataInput;->readEntityData([BII)I

    .line 114
    invoke-static {p2, v0}, Lcom/melesta/engine/DbBackup;->restoreFiles(Lcom/melesta/engine/DatabaseHelper;[B)V

    .line 115
    return-void
.end method

.method private restoreTransactions(Landroid/app/backup/BackupDataInput;Lcom/melesta/engine/DatabaseHelper;I)V
    .locals 2
    .parameter "data"
    .parameter "dbh"
    .parameter "dataSize"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 118
    new-array v0, p3, [B

    .line 119
    .local v0, dataBuf:[B
    const/4 v1, 0x0

    invoke-virtual {p1, v0, v1, p3}, Landroid/app/backup/BackupDataInput;->readEntityData([BII)I

    .line 121
    invoke-static {p2, v0}, Lcom/melesta/engine/DbBackup;->restoreTransactions(Lcom/melesta/engine/DatabaseHelper;[B)V

    .line 122
    return-void
.end method


# virtual methods
.method public onBackup(Landroid/os/ParcelFileDescriptor;Landroid/app/backup/BackupDataOutput;Landroid/os/ParcelFileDescriptor;)V
    .locals 8
    .parameter "oldState"
    .parameter "data"
    .parameter "newState"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 29
    :try_start_0
    const-string v6, "engine"

    const-string v7, "BackupAgent-store"

    invoke-static {v6, v7}, Landroid/util/Log;->i(Ljava/lang/String;Ljava/lang/String;)I

    .line 31
    invoke-static {p0}, Lcom/melesta/engine/DatabaseHelper;->getHelper(Landroid/content/Context;)Lcom/melesta/engine/DatabaseHelper;

    move-result-object v0

    .line 33
    .local v0, dbh:Lcom/melesta/engine/DatabaseHelper;
    invoke-static {p2, v0}, Lcom/melesta/engine/BackupAgent;->backupFiles(Landroid/app/backup/BackupDataOutput;Lcom/melesta/engine/DatabaseHelper;)[B

    .line 34
    invoke-static {p2, v0}, Lcom/melesta/engine/BackupAgent;->backupTransactions(Landroid/app/backup/BackupDataOutput;Lcom/melesta/engine/DatabaseHelper;)[B

    .line 37
    new-instance v5, Ljava/io/FileOutputStream;

    invoke-virtual {p3}, Landroid/os/ParcelFileDescriptor;->getFileDescriptor()Ljava/io/FileDescriptor;

    move-result-object v6

    invoke-direct {v5, v6}, Ljava/io/FileOutputStream;-><init>(Ljava/io/FileDescriptor;)V

    .line 38
    .local v5, outstream:Ljava/io/FileOutputStream;
    new-instance v4, Ljava/io/DataOutputStream;

    invoke-direct {v4, v5}, Ljava/io/DataOutputStream;-><init>(Ljava/io/OutputStream;)V

    .line 40
    .local v4, out:Ljava/io/DataOutputStream;
    invoke-static {}, Ljava/lang/System;->currentTimeMillis()J

    move-result-wide v2

    .line 41
    .local v2, modified:J
    invoke-virtual {v4, v2, v3}, Ljava/io/DataOutputStream;->writeLong(J)V

    .line 42
    invoke-virtual {v4}, Ljava/io/DataOutputStream;->close()V

    .line 43
    invoke-virtual {v5}, Ljava/io/FileOutputStream;->close()V
    :try_end_0
    .catch Ljava/lang/Exception; {:try_start_0 .. :try_end_0} :catch_0

    .line 47
    .end local v0           #dbh:Lcom/melesta/engine/DatabaseHelper;
    .end local v2           #modified:J
    .end local v4           #out:Ljava/io/DataOutputStream;
    .end local v5           #outstream:Ljava/io/FileOutputStream;
    :goto_0
    return-void

    .line 44
    :catch_0
    move-exception v1

    .line 45
    .local v1, ex:Ljava/lang/Exception;
    invoke-static {v1}, Lcom/melesta/engine/Error;->processException(Ljava/lang/Exception;)V

    goto :goto_0
.end method

.method public onRestore(Landroid/app/backup/BackupDataInput;ILandroid/os/ParcelFileDescriptor;)V
    .locals 11
    .parameter "data"
    .parameter "appVersionCode"
    .parameter "newState"
    .annotation system Ldalvik/annotation/Throws;
        value = {
            Ljava/io/IOException;
        }
    .end annotation

    .prologue
    .line 75
    :try_start_0
    const-string v8, "engine"

    new-instance v9, Ljava/lang/StringBuilder;

    invoke-direct {v9}, Ljava/lang/StringBuilder;-><init>()V

    const-string v10, "BackupAgent-restore "

    invoke-virtual {v9, v10}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v9

    invoke-virtual {p3}, Landroid/os/ParcelFileDescriptor;->toString()Ljava/lang/String;

    move-result-object v10

    invoke-virtual {v9, v10}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v9

    invoke-virtual {v9}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

    move-result-object v9

    invoke-static {v8, v9}, Landroid/util/Log;->i(Ljava/lang/String;Ljava/lang/String;)I

    .line 77
    invoke-static {p0}, Lcom/melesta/engine/DatabaseHelper;->getHelper(Landroid/content/Context;)Lcom/melesta/engine/DatabaseHelper;

    move-result-object v1

    .line 80
    .local v1, dbh:Lcom/melesta/engine/DatabaseHelper;
    const/4 v8, 0x0

    invoke-virtual {v1, v8}, Lcom/melesta/engine/DatabaseHelper;->getFilesList(Z)Ljava/util/List;

    move-result-object v8

    invoke-interface {v8}, Ljava/util/List;->size()I

    move-result v8

    if-lez v8, :cond_0

    .line 108
    .end local v1           #dbh:Lcom/melesta/engine/DatabaseHelper;
    :goto_0
    return-void

    .line 86
    .restart local v1       #dbh:Lcom/melesta/engine/DatabaseHelper;
    :cond_0
    :goto_1
    invoke-virtual {p1}, Landroid/app/backup/BackupDataInput;->readNextHeader()Z

    move-result v8

    if-eqz v8, :cond_2

    .line 87
    invoke-virtual {p1}, Landroid/app/backup/BackupDataInput;->getKey()Ljava/lang/String;

    move-result-object v3

    .line 88
    .local v3, key:Ljava/lang/String;
    invoke-virtual {p1}, Landroid/app/backup/BackupDataInput;->getDataSize()I

    move-result v0

    .line 89
    .local v0, dataSize:I
    const-string v8, "engine"

    new-instance v9, Ljava/lang/StringBuilder;

    invoke-direct {v9}, Ljava/lang/StringBuilder;-><init>()V

    const-string v10, "BackupAgent-restore("

    invoke-virtual {v9, v10}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v9

    invoke-virtual {v9, v3}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v9

    const-string v10, ") "

    invoke-virtual {v9, v10}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

    move-result-object v9

    invoke-virtual {v9, v0}, Ljava/lang/StringBuilder;->append(I)Ljava/lang/StringBuilder;

    move-result-object v9

    invoke-virtual {v9}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;

    move-result-object v9

    invoke-static {v8, v9}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String;)I

    .line 91
    const-string v8, "BACKUP"

    invoke-virtual {v8, v3}, Ljava/lang/String;->equals(Ljava/lang/Object;)Z

    move-result v8

    if-eqz v8, :cond_1

    .line 92
    invoke-direct {p0, p1, v1, v0}, Lcom/melesta/engine/BackupAgent;->restoreFiles(Landroid/app/backup/BackupDataInput;Lcom/melesta/engine/DatabaseHelper;I)V
    :try_end_0
    .catch Ljava/lang/Exception; {:try_start_0 .. :try_end_0} :catch_0

    goto :goto_1

    .line 105
    .end local v0           #dataSize:I
    .end local v1           #dbh:Lcom/melesta/engine/DatabaseHelper;
    .end local v3           #key:Ljava/lang/String;
    :catch_0
    move-exception v2

    .line 106
    .local v2, e:Ljava/lang/Exception;
    invoke-static {v2}, Lcom/melesta/engine/Error;->processException(Ljava/lang/Exception;)V

    goto :goto_0

    .line 93
    .end local v2           #e:Ljava/lang/Exception;
    .restart local v0       #dataSize:I
    .restart local v1       #dbh:Lcom/melesta/engine/DatabaseHelper;
    .restart local v3       #key:Ljava/lang/String;
    :cond_1
    :try_start_1
    const-string v8, "BACKUP2"

    invoke-virtual {v8, v3}, Ljava/lang/String;->equals(Ljava/lang/Object;)Z

    move-result v8

    if-eqz v8, :cond_0

    .line 94
    invoke-direct {p0, p1, v1, v0}, Lcom/melesta/engine/BackupAgent;->restoreTransactions(Landroid/app/backup/BackupDataInput;Lcom/melesta/engine/DatabaseHelper;I)V

    goto :goto_1

    .line 98
    .end local v0           #dataSize:I
    .end local v3           #key:Ljava/lang/String;
    :cond_2
    new-instance v7, Ljava/io/FileOutputStream;

    invoke-virtual {p3}, Landroid/os/ParcelFileDescriptor;->getFileDescriptor()Ljava/io/FileDescriptor;

    move-result-object v8

    invoke-direct {v7, v8}, Ljava/io/FileOutputStream;-><init>(Ljava/io/FileDescriptor;)V

    .line 99
    .local v7, outstream:Ljava/io/FileOutputStream;
    new-instance v6, Ljava/io/DataOutputStream;

    invoke-direct {v6, v7}, Ljava/io/DataOutputStream;-><init>(Ljava/io/OutputStream;)V

    .line 101
    .local v6, out:Ljava/io/DataOutputStream;
    invoke-static {}, Ljava/lang/System;->currentTimeMillis()J

    move-result-wide v4

    .line 102
    .local v4, modified:J
    invoke-virtual {v6, v4, v5}, Ljava/io/DataOutputStream;->writeLong(J)V

    .line 103
    invoke-virtual {v6}, Ljava/io/DataOutputStream;->close()V

    .line 104
    invoke-virtual {v7}, Ljava/io/FileOutputStream;->close()V
    :try_end_1
    .catch Ljava/lang/Exception; {:try_start_1 .. :try_end_1} :catch_0

    goto :goto_0
.end method
